local hx__miaoce = fk.CreateSkill {
  name = "hx__miaoce",
}

hx__miaoce:addEffect(fk.AfterSkillEffect, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    if target ~= player or not player:hasSkill(hx__miaoce.name) or data.skill.name ~= "hx__yiji" or player:usedEffectTimes(hx__miaoce.name) >= 20 then return false end
    local room = player.room
    local cards = player:getCardIds("h")
    if table.every(cards, function(id) return #cards == 0 or Fk:getCardById(id).color == Fk:getCardById(cards[1]).color end)
      or table.every(cards, function(id) return #cards == 0 or Fk:getCardById(id).type == Fk:getCardById(cards[1]).type end) then
      return true
    end
    return false
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = hx__miaoce.name,
      prompt = "#hx__miaoce-invoke",
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local judge = {
      who = player,
      reason = hx__miaoce.name,
      pattern = ".|.|.",
    }
    room:judge(judge)
    if judge.card.color == Card.Red then
      local targets = room:askToChoosePlayers(player, {
        targets = table.filter(room.alive_players, Util.IdMapper),
        min_num = 1,
        max_num = 1,
        prompt = "#hx__miaoce-ask-red",
        skill_name = hx__miaoce.name,
        cancelable = true
      })
      if #targets > 0 then
        room:damage({
          from = player,
          to = targets[1],
          damage = 1,
          skillName = hx__miaoce.name,
        })
      end
    elseif judge.card.color == Card.Black then
      local targets = room:askToChoosePlayers(player, {
        targets = table.filter(room.alive_players, Util.IdMapper),
        min_num = 1,
        max_num = 1,
        prompt = "#hx__miaoce-ask-black",
        skill_name = hx__miaoce.name,
        cancelable = true,
      })
      if #targets > 0 then
        local id1 = room:askToChooseCard(player, {
          target = targets[1],
          flag = "he",
          skill_name = hx__miaoce.name,
          prompt = "#hx__miaoce-discard1::"..targets[1].id
        })
        room:throwCard({id1}, hx__miaoce.name, targets[1], player)
        if targets[1]:isNude() then return end
        local id2 = room:askToChooseCard(player, {
          target = targets[1],
          flag = "he",
          skill_name = hx__miaoce.name,
          prompt = "#hx__miaoce-discard2::"..targets[1].id
        })
        room:throwCard({id2}, hx__miaoce.name, targets[1], player)
      end
    end
  end,
})

return hx__miaoce